<template>
 <div class="home-container">
    <!-- 导航栏 原本只能通过title属性设置一个导航栏的标题，现在添加一个搜索按钮，需要插槽-->
    <!-- 这样的效果就是将原本标题的位置，换成一个按钮进行显示 -->
    <van-nav-bar class="page-nav-bar" fixed>
        <!--增加一个名字为title的插槽 -->
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 频道栏 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab 
      :title="channel.name"
      v-for="channel in channels"
      :key="channel.id">
       <!-- 频道列表 -->
      <article-list :channel="channel"/>
      <!-- 频道列表 -->
      </van-tab>
    <!-- 频道栏 -->
      
      <!-- 这个是占位符 目的:让最后一个标签完整显示 -->
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isChennelEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
     
    </van-tabs>
    <!-- 频道栏 -->
    <!-- 弹出层 -->
      <van-popup
      v-model="isChennelEditShow"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
      close-icon-position="top-left"
      >
      <!-- 弹出层 -->
      <!-- 父传子 频道栏列表 -->
      <channel-edit 
      :active="active" 
      :my-channels="channels" 
      @update-active="onUpdateActive"/>
      </van-popup>
  </div>
</template>

<script>
import {getUserChannels} from '@/api/user'
// 引入写好的模板
import ArticleList from './components/article-list.vue'
// 弹出层组件
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
     ArticleList,
     ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,//默认显示的页
      channels: [],//频道列表
      isChennelEditShow: false, // 这里我们先设置为 true 就能看到弹窗的页面了
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadChannels();
  },
  mounted () {},
  methods: {
    async loadChannels(){
      try{
        let channels=[]
        const localChannels=getItem('TOUTIAO_CHANNELS')
        if (this.user || !localChannels) {
          const {data}= await  getUserChannels();
        // console.log(data);
        channels=data.data.channels;
        }else { 
          channels = localChannels
        
        }
      this.channels = channels
      }catch(err){
        console.log(err);
        this.$toast('获取频道失败')
      }
    },
    onUpdateActive(index,isChennelEditShow=true){
      this.active=index;
      this.isChennelEditShow=isChennelEditShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top:174px;
  padding-bottom: 100px;
  // 加上deep让其被深度监听 去除vant-v
   /deep/ .van-nav-bar__title { 
    max-width: unset;
  } 
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {  
    .van-tabs__wrap {   //这里只需要给父节点channel-tabs添加/deep/,这样下边的子节点都会生效
      position:fixed;
      top: 92px;
      z-index:1;
      left: 0;
      right: 0;
      height: 82px;
    }

    .van-tab { 
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {  
      color: #333333;
    }

    .van-tabs__nav {  
      padding-bottom: 0;
    }

    .van-tabs__line { 
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .placeholder{
      flex-shrink: 0;//表示不占位
      width:66px;
      height:82px;
    }
    .hamburger-btn{
      position: fixed;
      right: 0;
      display: flex;
      justify-content:center;
      align-items: center;
       width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
    }
    &:before{//找到父节点，添加伪元素（这个是汉堡按钮与tab之间的一个竖线）
        content: "";
        position: absolute;
        left: 0;
        width: 1px;//在模拟器上看不到（手机上没问题），如果想看到改为2px
        height: 100%;
        // background-image: url(~@/assets/gradient-gray-line.png);
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
    }
}
}
</style>
